package com.admin.interfaces.web;

import com.admin.application.BannerService;
import com.admin.domain.modle.Banner;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * description 广告管理
 * User: WeiDa
 * Date: 2017-4-11
 * Time: 16:41
 */
@Controller
@RequestMapping("/banner")
public class BannerController {

    @Autowired
    protected BannerService bannerService;

    @RequestMapping(method = RequestMethod.POST, value = "/add")
    public String create(Banner banner) {
        bannerService.create(banner);
        return "redirect:/banner";
    }

    @RequestMapping(value = "/{id}/modify", method = RequestMethod.POST)
    public String modify(@PathVariable("id") String id, Banner banner) {
        banner.setId(id);
        bannerService.modify(banner);
        return "redirect:/banner";
    }


    @RequestMapping(value = "/{id}/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public void delete(@PathVariable("id") String id) {
        bannerService.delete(id);
    }

    @RequestMapping(value = "/form", method = RequestMethod.GET)
    public String toform(@RequestParam(name = "id", required = false) String id, Model model) {
        String api = "/banner/add";
        if (StringUtils.isNotBlank(id)) {
            model.addAttribute("banner", bannerService.get(id));
            api = "/banner/" + id + "/modify";
        }
        model.addAttribute("api", api);
        return "banner/form";
    }

    @RequestMapping(method = RequestMethod.GET)
    public String list(Model model, @RequestParam(value = "key", required = false) String key) {
        model.addAttribute("key", key);
        model.addAttribute("list", bannerService.list(key));
        return "banner/list";
    }


}
